<?php

namespace app\logic\single;


use app\logic\model\user\UserGoodsLogicModel;
use app\logic\model\user\UserMusicLogicModel;

class UserGoodsSingle
{


    private static $inc;
    /**
     * @var UserGoodsLogicModel
     */
    private $userGoodsModel;


    private function __clone(){

}

    public static function getInstance(){
        if(is_null(self::$inc)){
            self::$inc = new self();
        }
        return self::$inc;
    }
    private function __construct()
    {
        $this->userGoodsModel = new UserGoodsLogicModel();
    }

    function save($user_id,$goods_id,$type,$source='订单'){
        $check = $this->userGoodsModel->where('user_id',$user_id)
            ->where('goods_id',$goods_id)
            ->where('type',$type)
            ->value('goods_id');
        if(empty($check)){
           $this->userGoodsModel->save([
               'user_id'=>$user_id,
               'goods_id'=>$goods_id,
               'type'=>$type,
               'source'=>$source,
               'create_time'=>date('Y-m-d H:i:s')
               ]);
        }
    }
    public function saves(array $user_goods_arr)
    {
        return $this->userGoodsModel->saveAll($user_goods_arr);
    }
    public function isHave($goods_id, $type, $user_id)
    {
        if(empty($goods_id) || empty($type) || empty($user_id)){
            return false;
        }
        return (bool)$this->userGoodsModel->where('goods_id', $goods_id)
            ->where('type', $type)
            ->where('user_id', $user_id)
            ->find();
    }
}